import time
import asyncio


async def run(url):
    print(f'向{url}抓取数据开始')
    await asyncio.sleep(2)
    print(f'向{url}抓取数据结束')
    return url


# asyncio.ensure_future(c)
async def main():
    task_list = []
    for i in range(3):
        c = run(i)
        task = asyncio.ensure_future(c)
        task.add_done_callback(call_back)
        task_list.append(task)
    await asyncio.wait(task_list)


# loop.create_task(c) 老写法
async def main2():
    task_list = []
    for i in range(3):
        c = run(i)
        task = loop.create_task(c)
        task.add_done_callback(call_back)
        task_list.append(task)
    await asyncio.wait(task_list)


# asyncio.create_task(c) 新写法
async def main3():
    task_list = []
    for i in range(3):
        c = run(i)
        task = asyncio.create_task(c)
        task.add_done_callback(call_back)
        task_list.append(task)
    await asyncio.wait(task_list)


def call_back(f):
    print('返回值：', f.result())


if __name__ == '__main__':
    t1 = time.time()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    print(time.time() - t1)
